import { cloneDeep } from "@pureadmin/utils";

/**
 * roles：页面级别权限，这里模拟二种 "admin"、"common"
 * admin：管理员角色
 * common：普通角色
 */
const SystemDealer = {
  path: "/mytBase/dealer/index",
  name: "SystemDealer",
  meta: {
    title: "menus.dealer",
    icon: "ri:user-line",
    rank: 99
  }
};
const SystemLessees = {
  path: "/mytBase/lessees/index",
  name: "SystemLessees",
  meta: {
    title: "menus.lessees",
    icon: "ri:user-star-line",
    rank: 100
  }
};
const SystemLesseesDetail = {
  path: "/mytBase/lessees/detail",
  name: "SystemLesseesDetail",
  meta: {
    title: "menus.lesseesDetail",
    showLink: false,
    hiddenTag: true
  }
};

/* const SystemUserClassify = {
  path: "/mytBase/userClassify/index",
  name: "UserClassify",
  meta: {
    title: "menus.userClassify",
    icon: "ri:contacts-line",
    rank: 101
  }
}; */
const SystemTerminal = {
  path: "/mytBase/terminal/index",
  name: "SystemTerminal",
  meta: {
    title: "menus.terminalManage",
    icon: "ri:cellphone-line",
    rank: 102
  }
};
/* const SystemSeed = {
  path: "/mytBase/systemSeed/index",
  name: "SystemSeed",
  meta: {
    title: "menus.systemSeed",
    icon: "ri:seedling-line",
    rank: 103
  }
}; */
//getMenuInfo 暂时不使用
async function _getMenuInfo(newData) {
  // 用于存储转换后的结果
  const data = cloneDeep(newData);
  const result = [];
  result.push(...buildTree(data, 0));
  for (const item of result) {
    if (!item.children) {
      delete item.name;
      item.children = [cloneDeep(item)];
    }
  }
  removeIframeToNodes(result);
  return result;
}

/**
 * 判断对应的阶段是否含有下级，如果有下级那么就移除对应的frameUrl地址信息
 * @param nodes 待处理节点
 */
function removeIframeToNodes(nodes) {
  nodes.forEach(node => {
    delete node.meta.name;
    // delete node.meta.id;
    // delete node.meta.parentId;
    if (node.children && node.children.length > 0) {
      delete node.meta.frameSrc;
      // delete node.name;
      removeIframeToNodes(node.children); // 递归处理子节点
    }
  });
}

// 递归函数，用于构建嵌套结构
function buildTree(items, parentId) {
  return items
    .filter(item => {
      if (item && item.parentId === parentId && item.menuType < 2) {
        return item;
      }
    })
    .map(item => {
      if (!item.path.startsWith("/")) {
        item.path = "/" + item.path;
      }
      const tempObj = cloneDeep(item);
      // tempObj.auths = item.auths ? item.auths.split(",") : [];
      // tempObj.roles = [tempObj.roles];
      if (tempObj.frameSrc.length == 0) {
        delete tempObj.frameSrc;
      }
      // delete tempObj.frameSrc;
      delete tempObj.auths;
      delete tempObj.path;
      delete tempObj.component;
      const result = {
        path: item.path,
        component: item?.component ?? "",
        name: item.name,
        meta: tempObj,
        children: buildTree(items, item.id)
      };
      if (result.component.length === 0) {
        delete result.component;
      }
      if (result.children.length === 0) {
        delete result.children;
      }
      return result;
    });
}

export const getAsyncRoutes = () => {
  return new Promise(async resolve => {
    const asyncRouterInfo = [];
    asyncRouterInfo.push(SystemDealer);
    asyncRouterInfo.push(SystemLessees);
    asyncRouterInfo.push(SystemLesseesDetail);
    //asyncRouterInfo.push(SystemUserClassify);
    asyncRouterInfo.push(SystemTerminal);
    //asyncRouterInfo.push(SystemSeed);
    resolve({
      success: true,
      data: asyncRouterInfo
    });
  });
};
